package Demo21;

/**
 * 山脉数组的顶峰索引
 * https://leetcode.cn/problems/peak-index-in-a-mountain-array/
 * 根据数组分析,数组的 顶峰的左边 满足 arr[mid] > arr[mid - 1] 右边满足 arr[mid] < arr[mid - 1]
 * 因为左边是包含顶峰的,不可以越过 mid 所以 left = mid
 * 右边是不包含顶峰的 所以 right = mid - 1
 */

class Solution {
    public int peakIndexInMountainArray(int[] arr) {
        int left = 1,right = arr.length - 2;
        while(left < right) {
            int mid = left + (right - left + 1) / 2;
            if(arr[mid] > arr[mid - 1]) {
                left = mid;
            }else {
                right = mid - 1;
            }
        }
        return left;
    }
}